﻿using ICSharpCode.AvalonEdit.CodeCompletion;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Editing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{

    /// <summary>
    /// 表示自动完成列表中的某个项目。
    /// </summary>
    public class CustomCompletionData : ICompletionData
    {
        public CustomCompletionData(string text, string description, string completeText = null)
        {
            this.Text = text;
            this.description = description;

            if (completeText == null)
            {
                this.completeText = this.Text;
            }
            else
            {
                this.completeText = completeText;
            }
        }

        private string completeText;
        /// <summary>
        /// 自动完成文本。
        /// </summary>
        public string CompleteText
        {
            get { return this.completeText; }
        }

        /// <summary>
        /// 自动完成项目的图标。
        /// </summary>
        public System.Windows.Media.ImageSource Image
        {
            get { return null; }
        }

        public string Text { get; private set; }

        // Use this property if you want to show a fancy UIElement in the list.
        public object Content
        {
            get { return this.Text; }
        }

        private string description;
        /// <summary>
        /// 此条目的描述信息。
        /// </summary>
        public object Description
        {
            get { return this.description; }
        }

        /// <summary>
        /// 自动完成（上屏）
        /// </summary>
        /// <param name="textArea">目标文本区域。</param>
        /// <param name="completionSegment">自动完成片段。</param>
        /// <param name="insertionRequestEventArgs">插入请求事件参数。</param>
        public void Complete(TextArea textArea, ISegment completionSegment,
            EventArgs insertionRequestEventArgs)
        {
            textArea.Document.Replace(completionSegment, this.CompleteText);
            OnComplete(this, new EventArgs());
        }

        /// <summary>
        /// 优先权。本例中不需要它。
        /// </summary>
        public double Priority
        {
            get { return 0; }
        }

        /// <summary>
        /// “自动完成”动作发生后触发此事件。
        /// </summary>
        public event EventHandler<EventArgs> Completed;

        /// <summary>
        /// 用于触发“Completed”事件。
        /// </summary>
        protected void OnComplete(object sender, EventArgs e)
        {
            if (this.Completed != null)
            {
                this.Completed(sender, e);
            }
        }
    }
}
